/**
 * Created by Administrator on 2017/8/7.
 */
let userDao = require('../mysql/userDao.js');
let robotDao = require('../mysql/robotDao.js');
let bluedSDK = require('../util/bluedSDK.js');
let msg = require('../util/msg.js');
let rpc = require('../util/rpc.js');

let info = module.exports;

info.search = function (userId, uuid, cid, data) {
    if (data.id >= 10000) {
        info.user(userId, uuid, cid, data);
    } else {
        info.robot(uuid, cid, data);
    }
};
info.user = function (userId, uuid, cid, data) {
    let userModel = new userDao();
    userModel.inId(data.id, function (err, res) {
        if (!res) {
            msg.send(cid, uuid, 'error', {'api': 'funcUserInfo', 'code': 10062});
            return false;
        }
        let userInfo = {
            'id': res.res.id,
            'name': res.res.name,
            'gold': res.res.gold,
            //'score': res.res.score,
            'avatar': res.res.avatar,
            'playTimes': res.res.playTimes,
            'playVictory': res.res.playVictory,
            'playWins': res.res.playWins,
            'praise': res.res.praise,
            'follow': false
        };
        if (data.accessToken && res.res.bluedId) {
            bluedSDK.uid(data.accessToken, res.res.bluedId, function (result) {
                if (result.code == 200) {
                    //userInfo.name = result.data[0].name;
                    //userInfo.avatar = result.data[0].avatar ? result.data[0].avatar : userInfo.avatar;
                    userInfo.age = result.data[0].age;
                    userInfo.height = result.data[0].height;
                    userInfo.weight = result.data[0].weight;
                    userInfo.city = result.data[0].city_settled;
                    //userInfo.relationship = result.data[0].relationship;
                    if (userId != data.id) {
                        userInfo.follow = true;
                    }
                    if (result.data[0].relationship == 4 ||
                        result.data[0].relationship == 8 ||
                        result.data[0].relationship == 12) {
                        userInfo.follow = false;
                    }
                    userInfo.fans = info.fans(result.data[0].relationship);
                }
                msg.send(cid, uuid, 'funcUserInfo', userInfo);
            });
        } else {
            msg.send(cid, uuid, 'funcUserInfo', userInfo);
        }
    });
};
info.robot = function (uuid, cid, data) {
    let robotModel = new robotDao();
    robotModel.inId(data.id, function (err, res) {
        if (!res) {
            msg.send(cid, uuid, 'error', {'api': 'funcUserInfo', 'code': 10062});
            return false;
        }
        msg.send(cid, uuid, 'funcUserInfo', {
            'id': res.res.id,
            'name': res.res.name,
            'gold': res.res.gold,
            'avatar': res.res.avatar,
            'playTimes': res.res.playTimes,
            'playVictory': res.res.playVictory,
            'playWins': res.res.playWins,
            'praise': 0,
            'follow': false
        });
    });
};
info.follow = function (userId, uuid, cid, data) {
    //if (userId == data.id) {
    //    msg.send(cid, uuid, 'error', {'api': 'funcUserFollow', 'code': 11004});
    //    return false;
    //}
    if (!data.accessToken) {
        msg.send(cid, uuid, 'error', {'api': 'funcUserFollow', 'code': 11003});
        return false;
    }
    let userModel = new userDao();
    userModel.inId(data.id, function (err, res) {
        if (!res) {
            msg.send(cid, uuid, 'error', {'api': 'funcUserFollow', 'code': 10062});
            return false;
        }
        let param = {};
        param.bluedId = data.bluedId;
        param.uid = res.res.bluedId;
        bluedSDK.followed(data.accessToken, param, function (result) {
            if (result.code != 200) {
                msg.send(cid, uuid, 'error', {'api': 'funcUserFollow', 'code': result.code});
            } else {
                msg.send(cid, uuid, 'funcUserFollow', {'fans': info.fans(result.data[0].relationship)});
            }
        });
    });
};
info.unFollow = function (userId, uuid, cid, data) {
    //if (userId == data.id) {
    //    msg.send(cid, uuid, 'error', {'api': 'funcUserFollow', 'code': 11004});
    //    return false;
    //}
    if (!data.accessToken) {
        msg.send(cid, uuid, 'error', {'api': 'funcUserUnFollow', 'code': 11003});
        return false;
    }
    let userModel = new userDao();
    userModel.inId(data.id, function (err, res) {
        if (!res) {
            msg.send(cid, uuid, 'error', {'api': 'funcUserUnFollow', 'code': 10062});
            return false;
        }
        let param = {};
        param.bluedId = data.bluedId;
        param.uid = res.res.bluedId;
        bluedSDK.unFollowed(data.accessToken, param, function (result) {
            if (result.code != 200) {
                msg.send(cid, uuid, 'error', {'api': 'funcUserUnFollow', 'code': result.code});
            } else {
                msg.send(cid, uuid, 'funcUserUnFollow', {'fans': info.fans(result.data[0].relationship)});
            }
        });
    });
};
info.fans = function (relationship) {
    switch (relationship) {
        case 0:
        case 2:
        case 4:
        case 8:
        case 12:
            return false;
        case 1:
        case 3:
            return true;
    }
};